package gamer.data;

import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class DatabaseOpera {
    private Statement statement = null;
    private Connection connection = null;
    private Boolean flag = true;
    FileOpera fio = new FileOpera();

    private void mysqlConnection() {
        try {
            connection = ((DataSource) (new InitialContext()).lookup("java:/comp/env/gs")).getConnection();
            statement = connection.createStatement();
        } catch (Exception e) {
            fio.logFile("数据库连接异常:" + e.getMessage());
        }
    }

    public boolean mysqlUpdate(String table, String revises, String conditions) {
        String sql = "update " + table + " set " + revises + " where " + conditions;
        try {
            mysqlConnection();
            fio.logFile("数据更新:" + sql);
            statement.execute(sql);
        } catch (SQLException e) {
            flag = false;
            fio.logFile("数据更新异常:" + e.getMessage());
        } finally {
            mysqlClose();
        }
        return flag;
    }

    public boolean mysqlInsert(String table, String keys, String values) {
        String sql = "insert into " + table +"("+ keys + ") values (" + values+")";
        try {
            mysqlConnection();
            fio.logFile("数据插入:" + sql);
            statement.execute(sql);
        } catch (SQLException e) {
            flag = false;
            fio.logFile("数据插入异常:" + e.getMessage());
        } finally {
            mysqlClose();
        }
        return flag;
    }

    public boolean mysqlDelete(String table, String conditions) {
        String sql = "delete from " + table + " where " + conditions;
        try {
            mysqlConnection();
            fio.logFile("数据删除:" + sql);
            statement.execute(sql);
        } catch (SQLException e) {
            flag = false;
            fio.logFile("数据删除异常:" + e.getMessage());
        } finally {
            mysqlClose();
        }
        return flag;
    }

    public HashMap<String, String> mysqlQuery(String keys, String tables, String conditions) {
        HashMap<String, String> data = new HashMap<String, String>();
        String sql = "select " + keys + " from " + tables + " where " + conditions;
        try {
            mysqlConnection();
            fio.logFile("数据查询:" + sql);
            ResultSet rSet = statement.executeQuery(sql);
            if(rSet.next())
                for (int i = 1; i <= rSet.getMetaData().getColumnCount(); i++)
                    data.put(rSet.getMetaData().getColumnName(i), rSet.getString(i));
            rSet.close();
        } catch (SQLException e) {
            fio.logFile("数据查询异常:" + e.getMessage());
        } finally {
            mysqlClose();
        }
        return data;
    }

    private void mysqlClose() {
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            fio.logFile("数据库关闭异常:" + e.getMessage());
        }
    }
}
